package Demo_zhuanshu01.X7;
public class Test1 {
    
    public static void main(String[] args) {
        Number n1 = new Number();
        for (int i = 1; i <= 10; i++) {          
            Thread t1 = new Thread(n1,"计算机" + i);
            t1.start();
        }
    }
}

 class Number implements Runnable{
    public static int[] arr = new int[2000];
    public Object ob = new Object();
    public static int sum = 0;
    public static int index = 0;


    @Override
    public void run() {
        for (int i = 1; i < 2001; i++) {
            arr[i-1] = i;
        }
        while (true){
            synchronized (ob){
                if (count()){
                    break;
                }
            }
        }
    }

    public synchronized boolean count(){
        if (index < 2000){
            //计算的和为sum加上arr[index]
            sum = sum + arr[index];
            //索引加1
            index++;
            System.out.println(Thread.currentThread().getName() + "正在加第" + index + "个数，和为" + sum);
            return false;
        }
        else {
            return true;
        }
    }
}
